<html>
<head>
    <link href="${ctx}/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="${ctx}/font-awesome/3.0.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="${ctx}/css/index.css" rel="stylesheet">
    <link href="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.css" rel="stylesheet">
    <script src="${ctx}/jquery/2.1.4/jquery.min.js"></script>
    <script src="${ctx}/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.js"></script>
    <style>
        .btn {
            font-size: 12px;
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <div id="toolbar" style="display: table">
        <div class="input-group" style="float: left; width: 200px; margin: 0 15px 10px 0;">
            <span class="input-group-addon">任务名称</span>
            <input type="text" class="form-control" id="title">
        </div>
        <div class="input-group" style="float: left; width: 200px; margin: 0 15px 10px 0;">
            <span class="input-group-addon">客户姓名</span>
            <input type="text" class="form-control" id="customer">
        </div>
        <div class="input-group" style="float: left; max-width: 380px;margin: 0 15px 10px 0; ">
            <span class="input-group-addon">创建时间</span>
            <input type="date" class="form-control"
                   style="position:relative; float:left; width:140px;border-right: none" id="startTime">
            <input type="date" class="form-control" style="position:relative; float:left; width:140px" id="endTime">
        </div>
        <div class="input-group" style="float: left; max-width: 200px; margin: 0 15px 10px 0;">
            <span class="input-group-addon">任务状态</span>
            <select id="status" name="status" class="form-control" style="">
                <option value="">全部</option>
                <#list Status as status>
                    <option value="${status}" <#if status=='On'>selected</#if>>${status.title}</option>
                </#list>
            </select>
        </div>
        <div style="float: left;">
            <button id="search_btn" class="btn btn-info" style="margin-right: 10px">立即查询</button>
            <button id="insert_btn" class="btn btn-success">发布任务</button>
        </div>


    </div>
    <table id="table"></table>
</div>

<script src="${ctx}/bootstrap-table/1.12.0/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="${ctx}/bootstrap-table/1.12.0/extensions/export/bootstrap-table-export.min.js"></script>
<script src="${ctx}/js/extends.js"></script>
<script>

    function queryParams(opt) {
        opt.title = $("#title").val();
        opt.status = $("#status").val();
        opt.startTime = $("#startTime").val();
        opt.endTime = $("#endTime").val();
        opt.customer = $("#customer").val();
        return opt;
    };

    var table = null;

    $(function () {
        table = $('#table')
        //加载表格
        table.bootstrapTable({
            url: '/rouse/list', //请求后台的URL（*）
            method: 'post', //请求方式（*）
            toolbar: '#toolbar', //工具按钮用哪个容器
            striped: true,
            dataType: "json",
            pagination: true,
            // "queryParamsType": "limit",
            singleSelect: false,
            contentType: "application/x-www-form-urlencoded",
            pageSize: 50,
            pageNumber: 1,
            search: false, //不显示 搜索框
            showColumns: false, //不显示下拉框（选择显示的列）
            sidePagination: "server", //服务端请求
            queryParams: queryParams,
            //minimunCountColumns: 2,
            pageList: [50, 100, 200, 500], //可供选择的每页的行数（*）
            //queryParams : oTableInit.queryParams,//传递参数（*）
            searchOnEnterKey: true,
            // search: true, //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
            strictSearch: true,
            //showColumns: true, //是否显示所有的列
            showRefresh: false, //是否显示刷新按钮
            minimumCountColumns: 2, //最少允许的列数
            clickToSelect: false, //是否启用点击选中行
            height: (document.body.clientHeight), //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "userId", //每一行的唯一标识，一般为主键列
            // showToggle: true, //是否显示详细视图和列表视图的切换按钮
            // cardView: false, //是否显示详细视图
            // detailView: false, //是否显示父子表
            // showExport: true, //是否显示导出
            exportDataType: "selected", //basic', 'all', 'selected'
            columns: [{
                checkbox: true
            }, {
                title: 'ID',
                width: '50px',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            }, {
                field: 'createTime',
                title: '创建时间',
                width: '125px',
                formatter: function (value, row, index) {
                    return new Date(value).format("yyyy-MM-dd HH:mm:ss");
                }
            }, {
                field: 'title',
                title: '任务名称',
                formatter: function (value, row, index) {
                    var header = '';
                    var appStore = '';
                    if (row.needCheck == 'True') {
                        header = '（需审核）';
                    } else {
                        header = '（无需审核）';
                    }
                    <#list appStores as appStore>
                    if (row.appStore == '${appStore.code}') {
                        appStore = '【${appStore.name}】';
                    }
                    </#list>

                    return header + appStore + value;
                }
            }, {
                field: 'status',
                title: '状态',
                width: '80px',
                formatter: function (value, row, index) {
                    if (row.status == 'Off') {
                        var btns = '下架';
                    } else {
                        var btns = '上架';
                    }
                    return btns;
                }
            }, {
                field: 'counts',
                title: '完成数量',
                width: '80px'
            }, {
                field: 'countss',
                title: '当天完成数量',
                width: '80px'
            }, {
                field: 'countsss',
                title: '选择完成数量',
                width: '80px'
            }, {
                field: 'type',
                title: '任务奖励',
                width: '120px',
                formatter: function (value, row, index) {
                    console.log(row);
                    var type = '';
                    var reward = '';
                    var worth = row.worth;
                    <#list types as type>
                    if (row.type == '${type}') {
                        type = '${type.title}';
                    }
                    </#list>
                    <#list rewards as reward>
                    if (row.reward == '${reward}') {
                        reward = '${reward.title}';
                    }
                    </#list>
                    return reward + worth + type;
                }
            }, {
                field: 'uuid',
                title: '操作',
                width: '200px',
                formatter: function (value, row, index) {
                    if (row.status == 'Off') {
                        var btns = '<a href="javascript:void(0);" onClick="edit(\'' + row.taskId + '\')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="del(\'' + row.taskId + '\')">删除</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="exports(\'' + row.taskId + '\')">导出</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="" onClick="start(\'' + row.taskId + '\')">上架</a>';
                    } else {
                        var btns = '<a href="javascript:void(0);" onClick="edit(\'' + row.taskId + '\')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="del(\'' + row.taskId + '\')">删除</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="exports(\'' + row.taskId + '\')">导出</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="" onClick="start(\'' + row.taskId + '\')">下架</a>';
                    }
                    ;
                    return btns;
                }
            }],
            responseHandler: function (res) {
                return {
                    "total": res.total, //总页数
                    "rows": res.rows //数据
                };
            }
        });

        // 查询
        $("#search_btn").click(function () {
            table.bootstrapTable('refresh');
        });


        // 新增
        $("#insert_btn").click(function () {
            window.parent.iw.dialog({
                size: 'lg',
                title: '发布任务',
                href: '/rouse/insert',
                closing: function () {
                    table.bootstrapTable('refresh');
                }
            });
        });

    });

    // 编辑
    var edit = function (taskid) {
        window.parent.iw.dialog({
            size: 'lg',
            title: '编辑任务',
            href: '/rouse/edit/' + taskid,
            closing: function () {
                table.bootstrapTable('refresh');
            }
        });
    };
    // 审核成功tablist

    var success = function (taskid, title) {
        window.parent.iw.open({
            id: taskid,
            href: '/rouse/checkList?_title=' + title + '&_status=CheckSuccess',
            title: title
        });
    };

    // 审核失败
    var failure = function (taskid, title) {
        window.parent.iw.open({
            id: taskid,
            href: '/rouse/checkList?_title=' + title + '&_status=CheckFailure',
            title: title
        })
    };
    //状态
    var start = function (taskid) {
        if (confirm("确定修改？")) {
            $.post("/rouse/start/", {
                taskid: taskid
            }, function (res) {
                alert(res);
                table.bootstrapTable('refresh');
            });
        }
    }

    // 删除
    var del = function (taskid) {
        if (confirm("确定删除？")) {
            $.post("/rouse/del/", {
                taskid: taskid
            }, function (res) {
                alert(res);
                table.bootstrapTable('refresh');
            });
        }
    };

    // 导出
    var exports = function (taskid) {
        layer.open({
            type: 2,
            area: ['600px', '500px'],
            fix: false, //不固定
            maxmin: true,
            shade: 0.4,
            title: '导出',
            content: '/export/excel/list?taskId='+taskid
        });
    };

    //保存下拉框的值
    function save() {
        selectIndex = document.getElementById("status").selectedIndex;
        document.cookie = 'selectIndex =' + selectIndex;
    }

    window.onload = function () {
        var cooki = document.cookie;
        if (cooki != "") {
            cooki = "{\"" + cooki + "\"}";
            cooki = cooki.replace(/\s*/g, "").replace(/=/g, '":"').replace(/;/g, '","');
            var json = eval("(" + cooki + ")"); //将coolies转成json对象
            document.getElementById("status").options[json.selectIndex].selected = true;
        } else
            save();
    }

</script>

</body>

</html>
